<template>
  <el-dialog
    :title="formData.id?'编辑部门':'新增部门'"
    :visible="dialogVisible"
    @close="onCancel"
  >
    <el-form ref="formRef" :model="formData" :rules="formRulse">
      <el-form-item label="部门名称" prop="name" label-width="120px">
        <el-input v-model="formData.name" placeholder="2-10个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code" label-width="120px">
        <el-input v-model="formData.code" placeholder="2-10个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="managerId" label-width="120px">
        <el-select v-model="formData.managerId" placeholder="请选择" style="width:100%">
          <el-option
            v-for="item in managerList"
            :key="item.id"
            :label="item.username"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce" label-width="120px">
        <el-input v-model="formData.introduce" type="textarea" placeholder="1-100个字符" />
      </el-form-item>
      <el-form-item style="text-align : center">
        <el-button type="primary" @click="onSubmit">确定</el-button>
        <el-button type="warning" @click="onCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { addDepartmentApi, getDepartmentApi, getDepartmentDetailApi, getManagerListApi, updateDepartmentApi } from '@/api/department'

export default {
  props: {
    dialogVisible: {
      type: Boolean,
      required: true
    },
    pNodeid: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      managerList: [],
      formData: {
        code: '',
        introduce: '',
        managerId: '',
        name: '',
        pid: ''
      },
      formRulse: {
        code: [{ required: true, message: '部门编码不能为空', trigger: 'blur' },
          {
            min: 2, max: 10, message: '部门编码的长度为2-10个字符', trigger: 'blur'
          },
          { validator: async(rules, value, callback) => {
            let res = await getDepartmentApi()
            if (this.formData.id) {
              res = res.filter(item => item.id !== this.formData.id)
            }
            if (res.some(item => item.code === value)) {
              callback(new Error('该编码已经存在了'))
            } else {
              callback()
            }
          },
          trigger: 'blur'
          }
        ], // 部门编码
        introduce: [{ required: true, message: '部门介绍不能为空', trigger: 'blur' }, {
          min: 1, max: 100, message: '部门介绍的长度为1-100个字符', trigger: 'blur'
        }], // 部门介绍
        managerId: [{ required: true, message: '部门负责人不能为空', trigger: 'blur' }], // 部门负责人id
        name: [{ required: true, message: '部门名称不能为空', trigger: 'blur' },
          {
            min: 2, max: 10, message: '部门名称的长度为2-10个字符', trigger: 'blur'
          },
          { validator: async(rules, value, callback) => {
            let res = await getDepartmentApi()
            if (this.formData.id) {
              res = res.filter(item => item.id !== this.formData.id)
            }
            if (res.some(item => item.name === value)) {
              callback(new Error('该部门已经存在了'))
            } else {
              callback()
            }
          },
          trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    this.getManagerList()
  },
  methods: {
    async getManagerList() {
      const res = await getManagerListApi()
      this.managerList = res
    },
    onSubmit() {
      this.$refs.formRef.validate(async(e) => {
        if (e) {
          if (this.formData.id) {
            await updateDepartmentApi(this.formData)
          } else {
            await addDepartmentApi({ ...this.formData, pid: this.pNodeid })
          }
          this.$message.success(`${this.formData.id ? '编辑' : '新增'}数据成功`)
          this.$emit('updataList')
          this.onCancel()
        }
      })
    },
    onCancel() {
      this.$refs.formRef.resetFields()
      this.$emit('update:dialogVisible', false)
      setTimeout(() => {
        this.formData = {
          code: '',
          introduce: '',
          managerId: '',
          name: '',
          pid: ''
        }
      }, 500)
    },
    async getDepartmentDetail(id) {
      const res = await getDepartmentDetailApi(id)
      this.formData = res
    }
  }
}
</script>

<style>
</style>
